package com.lalameow.packet.packImpl;

import com.lalameow.packet.BasePack;
import com.lalameow.packet.enumtype.PackType;
import com.lalameow.serverlogingate.enetiy.PlayerInfo;
import com.lalameow.serverlogingate.service.PlayerInfoService;
import com.lalameow.serverlogingate.utiles.ApplicationContextUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2018/2/26
 * 时间: 9:52
 * 功能：请进行修改
 */
@Data
public class EditPswPack extends PacketAbstract{
    private String username;
    private String oldPwd;
    private String newPwd;

    @Override
    public BasePack revice(ChannelHandlerContext ctx) {
        PlayerInfoService playerInfoService=(PlayerInfoService) ApplicationContextUtil.getContext().getBean("playerInfoService");
        PlayerInfo playerInfo=playerInfoService.findByUserNameAndPwd(username,oldPwd);
        BasePack basePack=new BasePack();
        basePack.setPackType(PackType.EDITPSWRECIVE);
        EditPswRecivePack editPswRecivePack=new EditPswRecivePack();
        if(playerInfo!=null){
            editPswRecivePack.setEditOk(true);
            editPswRecivePack.setMsg("§2密码修改成功.");
            playerInfo.setPassword(newPwd);
            playerInfoService.save(playerInfo);
            basePack.setBodyContent(editPswRecivePack.toJsonStr());
            return basePack;
        }
        editPswRecivePack.setEditOk(false);
        editPswRecivePack.setMsg("§c密码或账号错误，修改失败！.");
        basePack.setBodyContent(editPswRecivePack.toJsonStr());
        return basePack;
    }
}
